package com.noble.glamour.ui.user.check.edit;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.noble.glamour.R;
import com.noble.glamour.bean.center.user.detail.UserDetail;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.base.BaseActivity;
import com.noble.glamour.module.util.ChineseFilter;
import com.noble.library.utils.UIUtil;
import com.noble.library.log.PToast;
import com.suke.widget.SwitchButton;

/**
 * 编辑内容 通用界面
 */
public class EditContentAct extends BaseActivity implements View.OnClickListener {
    private int intentType;
    private EditText editText;
    private LinearLayout authSetting;

    private UserDetail userDetail;
    private String defaultValue; // 初始value
    private int defaultAuth;     // 初始权限值
    private int newAuth;         // 更改权限值

    private boolean isSaved; // 是否保存了更改信息
    private SwitchButton seekbar;
    private boolean sb_status;
    private TextView tv_auth_tip, tv_tip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p1_user_edit_content_act);
        setBackView();
        setTitleRight("保存", this);

        initData();
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        isSaved = false;
    }

    private void initData() {
        userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        intentType = getIntent().getIntExtra("intentType", EditKey.EDIT_NICK_NAME);
        defaultValue = getIntent().getStringExtra("defaultValue");

        switch (intentType) {
            case EditKey.EDIT_CONNECTION_PHONE:
//                defaultAuth = userDetail.getMobileAuth();
                break;

            case EditKey.EDIT_CONNECTION_QQ:
//                defaultAuth = userDetail.getQQAuth();
                break;

            case EditKey.EDIT_CONNECTION_WECHAT:
//                defaultAuth = userDetail.getWechatAuth();
                break;
        }

    }

    // 初始化 SeekBar
    private void initSeekBar() {
        if (defaultAuth != 2) {  // 公开
            sb_status = true;
            seekbar.setChecked(true);
        } else {                 // 保密
            sb_status = false;
            seekbar.setChecked(false);
        }
    }

    private void setAuth(int auth) {
        switch (intentType) {
            case EditKey.EDIT_CONNECTION_PHONE:
//                userDetail.setMobileAuth(auth);
                break;
            case EditKey.EDIT_CONNECTION_QQ:
//                userDetail.setQQAuth(auth);
                break;
            case EditKey.EDIT_CONNECTION_WECHAT:
//                userDetail.setWechatAuth(auth);
                break;
        }
    }

    private void initView() {
        tv_auth_tip = (TextView) findViewById(R.id.tv_auth_tip);
        tv_tip = (TextView) findViewById(R.id.tv_tip);
        tv_auth_tip.setText(userDetail.isMan() ? "公开" : "仅对VIP公开");
        seekbar = (SwitchButton) findViewById(R.id.seekbar);
        authSetting = (LinearLayout) findViewById(R.id.view_confidentiality);
        seekbar.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                sb_status = isChecked;
            }
        });
        initSeekBar();

        editText = (EditText) findViewById(R.id.edit_content_text);
        switch (intentType) {
            case EditKey.EDIT_NICK_NAME:
                authSetting.setVisibility(View.GONE);
                initTitle("填写昵称", "请输入您的昵称", defaultValue);
                tv_tip.setText("昵称:");
                break;
            case EditKey.EDIT_CONNECTION_PHONE:
                initTitle("填写手机号", "请输入您的手机号", defaultValue);
                tv_tip.setText("手机号:");
                break;
            case EditKey.EDIT_CONNECTION_QQ:
                initTitle("填写QQ号", "请输入您的QQ号码", defaultValue);
                tv_tip.setText("QQ:");
                break;
            case EditKey.EDIT_CONNECTION_WECHAT:
                initTitle("填写微信号", "请输入您的微信号", defaultValue);
                tv_tip.setText("微信号:");
                break;
        }
    }

    private void initTitle(String title, String hint, String defaultValue) {
        setTitle(title);
        setListenerUseType(intentType);
        editText.setHint(hint);
        if (!TextUtils.isEmpty(defaultValue)) {
            editText.setText(defaultValue);
            UIUtil.endCursor(editText);
        }
    }

    @Override
    public void onClick(View v) {
        if (test()) {
            isSaved = true;
            saveAndFinish();
        }
    }

    /**
     * 设置输入限制
     */
    private void setListenerUseType(int type) {
        switch (type) {
            case EditKey.EDIT_NICK_NAME:
                ModuleMgr.getCenterMgr().inputFilterSpace(editText, 7);
                break;
            case EditKey.EDIT_CONNECTION_PHONE:
                editText.setInputType(InputType.TYPE_CLASS_PHONE);
                editText.addTextChangedListener(phoneTextWatcher);
                break;
            case EditKey.EDIT_CONNECTION_QQ:
                editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
            case EditKey.EDIT_CONNECTION_WECHAT:
                editText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
                editText.addTextChangedListener(weChatTextWatcher);
                break;
        }
    }

    // 微信输入监听
    private final TextWatcher weChatTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String editable = editText.getText().toString();
            String str = ChineseFilter.chineseFilter(editable); // 过滤中文字符
            if (!editable.equals(str)) {
                editText.setText(str);
                UIUtil.endCursor(editText);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    // 手机号输入监听
    private final TextWatcher phoneTextWatcher = new TextWatcher() {
        private int cou = 0;
        int selectionEnd = 0;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            cou = before + count;
            String editable = editText.getText().toString();
            String str = ChineseFilter.stringFilter(editable); //过滤特殊字符
            if (!editable.equals(str)) {
                editText.setText(str);
            }
            editText.setSelection(editText.length());
            cou = editText.length();
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (cou > 11) {
                selectionEnd = editText.getSelectionEnd();
                s.delete(11, selectionEnd);
            }
        }
    };

    /**
     * 检测输入数据是否合法
     *
     * @return
     */
    private boolean test() {
        String contact = editText.getText().toString().trim();
        if (TextUtils.isEmpty(contact)) {
            return false;
        }

        switch (intentType) {
            case EditKey.EDIT_NICK_NAME:
                if (contact.length() < 2) {
                    PToast.showShort("昵称至少2个字符");
                    return false;
                }
                break;
            case EditKey.EDIT_CONNECTION_PHONE:
                if (!ChineseFilter.isPhoneNumber(contact)) {
                    PToast.showShort("手机号码格式错误");
                    return false;
                }
                break;

            case EditKey.EDIT_CONNECTION_QQ:
                if (!ChineseFilter.isQQNumber(contact)) {
                    PToast.showShort("QQ号码格式错误");
                    return false;
                }
                break;
            case EditKey.EDIT_CONNECTION_WECHAT:
                if (!ChineseFilter.isWeChatNumber(contact)) {
                    PToast.showShort("微信账号格式错误");
                    return false;
                }
                break;
        }
        return true;
    }

    /**
     * 保存修改信息值，并关闭当前页面
     */
    private void saveAndFinish() {
        setAuth(sb_status ? (userDetail.isMan() ? 0 : 1) : 2);
        String contact = editText.getText().toString().trim();
        if (isSaved) {
            switch (intentType) {
                case EditKey.EDIT_NICK_NAME:
                    if (!contact.equals(defaultValue)) {
                        Intent intent = new Intent();
                        intent.putExtra("newValue", contact);
                        setResult(EditKey.UPDATE_NICK_NAME, intent);
                    }
                    break;
                case EditKey.EDIT_CONNECTION_PHONE:
//                    newAuth = ModuleMgr.getCenterMgr().getMyInfo().getMobileAuth();
                    if (!contact.equals(defaultValue) || defaultAuth != newAuth) {
                        Intent intent = new Intent();
                        intent.putExtra("newValue", contact);
                        intent.putExtra("newAuth", newAuth);
                        setResult(EditKey.UPDATE_CONNECTION_PHONE, intent);
                    }
                    break;
                case EditKey.EDIT_CONNECTION_QQ:
//                    newAuth = ModuleMgr.getCenterMgr().getMyInfo().getQQAuth();
                    if (!contact.equals(defaultValue) || defaultAuth != newAuth) {
                        Intent intent = new Intent();
                        intent.putExtra("newValue", contact);
                        intent.putExtra("newAuth", newAuth);
                        setResult(EditKey.UPDATE_CONNECTION_QQ, intent);
                    }

                    break;
                case EditKey.EDIT_CONNECTION_WECHAT:
//                    newAuth = ModuleMgr.getCenterMgr().getMyInfo().getWechatAuth();
                    if (!contact.equals(defaultValue) || defaultAuth != newAuth) {
                        Intent intent = new Intent();
                        intent.putExtra("newValue", contact);
                        intent.putExtra("newAuth", newAuth);
                        setResult(EditKey.UPDATE_CONNECTION_WECHAT, intent);
                    }
                    break;
            }
            finish();
        }
    }
}
